#include "StdAfx.h"
#include "AnyApplication.h"
#include "OCAFApplication.h"
#include "GraphicDevice.h"
#include "AnyDocument.h"

CAnyApplicaton::CAnyApplicaton()
{
	m_pOcafApp = new TOcaf_Application();
}

CAnyApplicaton::~CAnyApplicaton()
{
	ExitApplication();

	delete m_pOcafApp;
	m_pOcafApp = NULL;
}

BOOL CAnyApplicaton::Initialize()
{
	//UnitsAPI::SetLocalSystem(UnitsAPI_MDTV);
	//GetEditorFactory().Initialize();
	bool bSuccess = CGraphicDevice::Initialize();
	if(!bSuccess)
	{
		AfxMessageBox(_T("Failed to initlize Graphic Device!"));
		return FALSE;
	}

	//m_Plugs.LoadPlug(_T("Primitive.dll"));
	//HINSTANCE  hModule = ::LoadLibrary(_T("Primitive.dll"));
	//if(NULL == hModule)
	//{
	//	AfxMessageBox(_T("Load Primitive.dll Error"));
	//}
	//else
	//{
	//	m_listPlugs.push_back(hModule);

	//	LoadPlugin pFun = (LoadPlugin)GetProcAddress(hModule, LOAD_FUNCTION_NAME);
	//	if(NULL == pFun)
	//	{
	//		AfxMessageBox(_T("Load Entry Fucntion Error!"));
	//	}
	//	else
	//	{
	//		pFun();
	//	} 
	//}

	return true;
}

void CAnyApplicaton::ExitApplication()
{
	//PlugList::iterator it = m_listPlugs.begin();
	//for(; it != m_listPlugs.end(); ++it)
	//{
	//	HINSTANCE  hModule = *it;
	//	UnLoadPlugin pFun = (UnLoadPlugin)GetProcAddress(hModule, UNLOAD_FUNCTION_NAME);
	//	if(NULL == pFun)
	//	{
	//		AfxMessageBox(_T("UnLoad Entry Fucntion Error!"));
	//	}
	//	else
	//	{
	//		pFun();
	//	} 
	//	::FreeLibrary(hModule);
	//}

	//m_listPlugs.clear();
}

CAnyDocument* CAnyApplicaton::CreateDocument()
{
	Handle_TDocStd_Document hDoc;
	m_pOcafApp->NewDocument(_T("AnyCAX"), hDoc);

	return new CAnyDocument(hDoc);
}